package com.yylending.las.personal.loan;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.Loader;
import android.view.View;
import android.view.ViewStub;
import android.widget.TextView;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterLoader;
import com.yyfax.infrastructure.statistics.StatService;
import com.yylending.las.R;
import com.yylending.las.StatIDSet;
import com.yylending.las.base.BaseActivity;
import com.yylending.las.common.utils.FormatUtils;
import com.yylending.las.common.utils.TimeUtils;
import com.yylending.las.common.widgets.HorizontalProgressBar;
import com.yylending.las.repay.LoanDetailModel;
import com.yylending.las.repay.LoanDetailPresenter;
import com.yylending.las.repay.repaydetail.RepayDetailActivity;

import java.util.List;

/**
 * 借款详情
 * Created by ying_he on 2017/6/5.<br>
 * Copyright  2017 www.yylending.com. All Rights Reserved.<br><br>
 */
public class LoanDetailActivity extends BaseActivity implements LoanDetailPresenter.View {

	private static final int LOADER_ID_LOAN_DETAIL = 1;
	public static final String KEY_INTENT_LOAN_NO = "key_intent_loan_no";
	public static final String KEY_INTENT_LOAN_TYPE = "key_intent_loan_type";

	TextView mLoanDateTv; // 借款日期
	TextView mLoanSumTv; // 借款总额
	ViewStub mUnderReviewViewStub; // 审核中ViewStub
	ViewStub mLoanDetailPayingViewStub; // 还款中ViewStub
	ViewStub mLoanDetailPayOffViewStub; // 已还清ViewStub
	TextView mCheckDebtEvidence; // 查看借据

	TextView mHasPayoffMoneyTv; // 已还清:金额

	TextView mShouldPay; // 审核中:每期应还
	TextView mFirstPayDateTv; // 审核中:首次还款日
	TextView mRepayPeriodTv; // 审核中:还款期数

	HorizontalProgressBar mHorizontalProgressView; // 还款中:还款进度指示条.
	TextView mCurrentShouldPay; // 还款中:本期应还
	TextView mRepayDateTv; // 还款中:还款日期

	private View mNoNetworkView;
	private TextView mRetryTv;

	private LoanDetailPresenter mLoanDetailPresenter;

	private String mLoanNo = "";
	private String mLoanType = "";


	public static void start(Activity aty, String loanNo, String type) {
		Intent intent = new Intent(aty, LoanDetailActivity.class);
		intent.putExtra(KEY_INTENT_LOAN_NO, loanNo);
		intent.putExtra(KEY_INTENT_LOAN_TYPE, type);
		aty.startActivity(intent);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_loan_detail);
		initData();
		getSupportLoaderManager().initLoader(LOADER_ID_LOAN_DETAIL, null, this);
	}

	@Override
	public Loader<Presenter> onCreateLoader(int id, Bundle args) {
		return new PresenterLoader<>(this, new LoanDetailPresenter.Generator());
	}

	@Override
	public void onLoadFinished(Loader<Presenter> loader, Presenter data, int placeholder) {
		mLoanDetailPresenter = (LoanDetailPresenter) data;
		mLoanDetailPresenter.attach(this);

		mLoanDetailPresenter.getLoanDetail(mLoanNo, mLoanType, true);
	}

	private void initData() {
		setTitle(R.string.loan_detail);

		setRightTextVisible(true);
		setRightTx(R.string.repay_detail);

		Intent intent = getIntent();
		mLoanNo = intent.getStringExtra(KEY_INTENT_LOAN_NO);
		mLoanType = intent.getStringExtra(KEY_INTENT_LOAN_TYPE);

		switch (mLoanType) {
			case LoanListActivity.LOAN_STATUS_CODE_IN_REVIEW:
				mUnderReviewViewStub = (ViewStub) findViewById(R.id.under_review_view_stub);
				mUnderReviewViewStub.inflate();
				break;
			case LoanListActivity.LOAN_STATUS_CODE_IN_REPAY:
				mLoanDetailPayingViewStub = (ViewStub) findViewById(R.id.loan_detail_paying_view_stub);
				mLoanDetailPayingViewStub.inflate();
				break;
			case LoanListActivity.LOAN_STATUS_CODE_PAY_OFF:
				mLoanDetailPayOffViewStub = (ViewStub) findViewById(R.id.loan_detail_pay_off_view_stub);
				mLoanDetailPayOffViewStub.inflate();
				break;
			default:
				break;
		}

		mLoanDateTv = (TextView) findViewById(R.id.loan_date_tv);
		mLoanSumTv = (TextView) findViewById(R.id.loan_sum_tv);

		mCheckDebtEvidence = (TextView) findViewById(R.id.check_debt_evidence);

		mHasPayoffMoneyTv = (TextView) findViewById(R.id.has_pay_off_money_tv);

		mShouldPay = (TextView) findViewById(R.id.should_pay);
		mFirstPayDateTv = (TextView) findViewById(R.id.first_pay_date_tv);
		mRepayPeriodTv = (TextView) findViewById(R.id.repay_period_tv);

		mHorizontalProgressView = (HorizontalProgressBar) findViewById(R.id.horizontal_progress_view);
		mCurrentShouldPay = (TextView) findViewById(R.id.current_should_pay);
		mRepayDateTv = (TextView) findViewById(R.id.repay_date_tv);

		mCheckDebtEvidence.setOnClickListener(this);

		mNoNetworkView = findViewById(R.id.no_network_layout);
		mRetryTv = (TextView) findViewById(R.id.retry_tv);
		mNoNetworkView.setOnClickListener(this);
		setRightTextVisible(false);
	}

	@Override
	public void getLoanDetailSuccess(List<LoanDetailModel.LoanDetail> result) {
		mNoNetworkView.setVisibility(View.GONE);
		mLoanDateTv.setText(TimeUtils.formatDateTimeSlash(Long.valueOf(result.get(0).loanDate)));
		mLoanSumTv.setText(String.format(getString(R.string.lending_sum), FormatUtils.getDecoratedAmountWithSymbol(result.get(0).loanAmount)));

		setRightTextVisible(true);
		switch (mLoanType) {
			case LoanListActivity.LOAN_STATUS_CODE_IN_REVIEW:
				setRightTextVisible(false);
				mShouldPay.setText(FormatUtils.getAmountWithSymbol(result.get(0).repayAmount));
				mFirstPayDateTv.setText(TimeUtils.formatDateTimeMonthZh(Long.valueOf(result.get(0).repayDate)));
				mRepayPeriodTv.setText(String.format(getString(R.string.left_pay_term), result.get(0).loanTerm));
				break;
			case LoanListActivity.LOAN_STATUS_CODE_IN_REPAY:
				// 还款总期数
				int totalPeriods = Integer.valueOf(result.get(0).loanTerm);
				// 当前还款期数.
				int currentRepayPeriod = totalPeriods - Integer.valueOf(result.get(0).repayTerm) + 1;
				mHorizontalProgressView.setTips(String.format(getString(R.string.loan_detail_current_repay_period), String.valueOf(currentRepayPeriod)))
						.setScale(currentRepayPeriod, totalPeriods, getString(R.string.unit_period))
						.setProgressListener(new HorizontalProgressBar.ProgressListener() {
							@Override
							public void currentProgressListener(float currentProgress) {
							}
						}).doAnimation(false);

				mCurrentShouldPay.setText(FormatUtils.getAmountWithSymbol(result.get(0).repayAmount));
				mRepayDateTv.setText(TimeUtils.formatDateTimeMonthZh(Long.valueOf(result.get(0).repayDate)));
				break;
			case LoanListActivity.LOAN_STATUS_CODE_PAY_OFF:
				mHasPayoffMoneyTv.setText(FormatUtils.getAmountWithSymbol(result.get(0).paidAmount));
				break;
			default:
				break;
		}
	}

	@Override
	public void getLoanDetailFailed(String msg) {
		mNoNetworkView.setVisibility(View.VISIBLE);
	}

	@Override
	public void onFunctionalError(Throwable e, boolean autoTips, int functionalCode) {
		super.onFunctionalError(e, autoTips, functionalCode);
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				mNoNetworkView.setVisibility(View.VISIBLE);
			}
		});
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		switch (v.getId()) {
			case R.id.check_debt_evidence:
				StatService.onEventWithSingleAttribute(this, StatIDSet.EVENT_ID_CHECK_IOU, mLoanType);
				DebtEvidenceActivity.start(this, mLoanNo, mLoanType);
				break;
			case R.id.no_network_layout:
				mLoanDetailPresenter.getLoanDetail(mLoanNo, mLoanType, true);
				break;
			default:
				break;
		}
	}

	@Override
	public void onRightBtnPressed() {
		StatService.onEventWithSingleAttribute(this, StatIDSet.EVENT_ID_CHECK_PAYMENT_DETAILS_2, mLoanType);
		// 跳转还款详情
		RepayDetailActivity.start(this, mLoanNo, "10");    //传过去的是借据号，type传10
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		getSupportLoaderManager().destroyLoader(LOADER_ID_LOAN_DETAIL);
	}
}
